package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    /**
     * 床位添加
     *
     * @param bedDto
     */
    @Override
    public void addBed(BedDto bedDto) {
        //1.将bedDto封装给Bed
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);

        //2.数据填充完善数据(公共字段已经有mybatis拦截器填充了)
        //bed_status 床位状态，默认为 0 代表未入住
        bed.setBedStatus(0);

        //3.调用mapper插入数据库
        bedMapper.insert(bed);
    }

    /**
     * 通过ID检索床位
     * @param id 床位ID
     * @return 床位视图对象
     */
    @Override
    public BedVo getBedById(Long id) {
        return BeanUtil.toBean(bedMapper.getBedById(id), BedVo.class);
    }

    /**
     * 更新床位信息
     *
     * @param bedDto 床位对象
     */
    @Override
    public void updateBed(BedDto bedDto) {
        Bed bed = new Bed();
        BeanUtil.copyProperties(bedDto,bed);
        bedMapper.updateBed(bed);
    }

    @Override
    public void deleteBedById(Long id) {
        bedMapper.deleteBedById(id);
    }
}

